package com.eastjet.equipment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastjet.equipment.service.*;
import com.eastjet.equipment.util.CodeRules;
import com.eastjet.maintenance.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RequestMapping("/spotcheckPlan")
@RestController
public class DeviceSpotcheckplanController {
    @Autowired
    private DevicePmcsService devicePmcsService;

    @Autowired
    private InspectionDetailsService inspectionDetailsService;
    @Autowired
    private DeviceRulesService deviceRulesService;
    @Autowired
    private DeviceSpotcheckService deviceSpotcheckService;
    @Autowired
    private DocumentationService documentationService;
    @Autowired
    private CodeRules codeRules;

    private String code;

    @GetMapping("/schemeList")
    public Page<DeviceSpotcheck> getSupplierList(@RequestParam(defaultValue = "1") int page,
                                                 @RequestParam(defaultValue = "2") int size,
                                                 @RequestParam(defaultValue = "") String condition,
                                                 @RequestParam(defaultValue = "") String type,
                                                 @RequestParam(defaultValue = "") String name
    ) {
        System.out.println("condition:" + condition);
        System.out.println("page:" + page);
        return deviceSpotcheckService.schemeList(page, size, condition, type,name);
    }

    @RequestMapping("/addPatrol")
    @Transactional
    public String add(@RequestBody Map<String, Object> map) throws ParseException {
        if (map.isEmpty()) {
            return "参数不能为空";
        }
        // 检查输入的表名称是否为空
        if (map.isEmpty()) {
            //返回错误信息
            return "TableName is null or empty";
        } else {
            System.out.println("1map:" + map);
            DeviceSpotcheck deviceSpotcheck = new DeviceSpotcheck();
            deviceSpotcheck.setCode(code());
            code = deviceSpotcheck.getCode();

            // 使用 Integer.valueOf 转换整数
            deviceSpotcheck.setSpotcheckName((String) map.get("patrolSchemeName"));
            deviceSpotcheck.setSpotcheckRoleId(Integer.valueOf(map.get("patrolSchemeRule").toString()));
            deviceSpotcheck.setSpotcheckPerson((String) map.get("patrolSchemePerson"));
            deviceSpotcheck.setSpotcheckDaytypeId((String) map.get("patrolSchemeDaytypeId"));
            deviceSpotcheck.setSpotcheckClassId((String) map.get("patrolSchemeClass"));
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date patrolSchemeOntime = sdf.parse((String) map.get("patrolSchemeOntime"));
            Date patrolSchemeUntime = sdf.parse((String) map.get("patrolSchemeUntime"));
            deviceSpotcheck.setSpotcheckState((String) map.get("patrolSchemeType"));
            deviceSpotcheck.setSpotcheckOntime(patrolSchemeOntime);
            deviceSpotcheck.setSpotcheckUntime(patrolSchemeUntime);
            deviceSpotcheck.setSpotcheckNotes((String) map.get("notas"));
            System.out.println("spot:" + deviceSpotcheck);
            return deviceSpotcheckService.addSpotcheck(deviceSpotcheck);
        }

    }

    public String code() {
        String TableName = "device_spotcheck";
        List<DeviceRules> list = deviceRulesService.getDeviceRulesByTableName(TableName);
        String s = codeRules.codeRules(list);
        return s;
    }

    @RequestMapping("/addDetail")
    @Transactional
    public String addDetail(@RequestBody Map<String, Object> map) {
        System.out.println("2map:" + map);
        System.out.println(code);
        if (map.isEmpty()) {
            //返回错误信息
            return "TableName is null or empty";
        } else {
            InspectionDetails inspectionDetails = new InspectionDetails();
            inspectionDetails.setCode((String) map.get("deviceCode"));
            inspectionDetails.setCodeType(code);
            inspectionDetails.setWhether(Integer.valueOf(map.get("isCritical").toString()));
            inspectionDetails.setCheckIn(Integer.valueOf(map.get("isScan").toString()));
            inspectionDetails.setProject(map.get("inspectionCode").toString());
            System.out.println("details:" + inspectionDetails);
            return inspectionDetailsService.addInspectionDetail(inspectionDetails);
        }

    }

    @RequestMapping("/addTask")
    @Transactional
    public String addTask(@RequestBody Map<String, Object> map) throws ParseException {
        System.out.println("111111" + code);
        if (map.isEmpty()) {
            return "参数不能为空";
        }
        // 检查输入的表名称是否为空
        if (map.isEmpty()) {
            //返回错误信息
            return "TableName is null or empty";
        } else {
            List<DeviceRules> list = deviceRulesService.getDeviceRulesByTableName("device_pmcs");
            DevicePmcs devicePmcs = new DevicePmcs();
            System.out.println("maps:" + map);

            devicePmcs.setTaskCoding(codeRules.codeRules(list));
            List<Object> tableData = (List<Object>) map.get("selectChange");
            System.out.println(tableData);
            tableData.forEach(item -> {
                System.out.println(item);
                devicePmcs.setCode(code);
                devicePmcs.setPmcsInspectiontasktypeId("未执行");
                devicePmcs.setPmcsName((String) map.get("patrolSchemeName"));
                devicePmcs.setPmcsNumber(1);
                System.out.println(((Map<?, ?>) item).get("numbering"));
                devicePmcs.setPmcsEquipmentId(String.valueOf(((Map<String, Object>) item).get("numbering")));
                devicePmcs.setInsertPerson((String) map.get("insertName"));
                devicePmcs.setInsertTime(new Date());
                DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date patrolSchemeOntime = null;
                Date patrolSchemeUntime = null;
                try {
                    patrolSchemeOntime = sdf.parse((String) map.get("patrolSchemeOntime"));
                    patrolSchemeUntime = sdf.parse((String) map.get("patrolSchemeUntime"));
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }

                devicePmcs.setPmcsOkontime(patrolSchemeOntime);
                devicePmcs.setPmcsOkuntime(patrolSchemeUntime);
                long time1 = patrolSchemeOntime.getTime();
                long time2 = patrolSchemeUntime.getTime();
                long diff = Math.abs(time2 - time1) / (60 * 1000);
                devicePmcs.setPmcsTime(String.valueOf(diff));
                devicePmcsService.addPmcs(devicePmcs);

            });


            return "";
        }


    }

    @RequestMapping("/adjust")
    public String adjust(@RequestBody Map<String, Object> map) {
        System.out.println("adjust:" + map);
        if (map.isEmpty()) {
            return "参数不能为空";
        } else {
            DeviceSpotcheck deviceSpotcheck = new DeviceSpotcheck();
            deviceSpotcheck.setCode((String) map.get("code"));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date updateTime = sdf.parse((String) map.get("updateTime"));
                deviceSpotcheck.setSpotcheckUntime(updateTime);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            deviceSpotcheck.setCause((String) map.get("cause"));
            deviceSpotcheck.setUpdatePerson((String) map.get("updateName"));
            deviceSpotcheck.setSpotcheckClassId((String) map.get("patrolschemeClass"));
            deviceSpotcheck.setSpotcheckPerson((String) map.get("patrolschemePerson"));
            return deviceSpotcheckService.update(deviceSpotcheck);
        }

    }

    @RequestMapping("/upload")
    public String upload(@RequestBody Map<String, Object> map) {

        Documentation documentation = new Documentation();
        documentation.setCode(code);
        documentation.setFilename((String) map.get("name"));
        documentation.setCreatedBy((String) map.get("uploader"));
        documentation.setCreationTime(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        try {
            Date uploadDate = sdf.parse((String) map.get("uploadDate"));
            documentation.setUploadTime(uploadDate);

        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        documentation.setUploader((String) map.get("uploader"));
        documentation.setSize((String) map.get("size"));
        System.out.println("document:" + map);
        System.out.println("documentssss:" + documentation);
        return documentationService.document(documentation);
    }

    @GetMapping("/selectPmts")
    public List<InspectionDetails> selectPmts(@RequestParam("")String code,@RequestParam("")String condition){
        return inspectionDetailsService.selectPmts(code,condition);
    }

    @GetMapping("/del")
    public String del(@RequestParam("")Integer id,@RequestParam("")String code){
        return deviceSpotcheckService.del(id,code);
    }
}

